<?php

class Emms_Form_Admin_PerformanceDataXtraLocation extends HTech_Form
{
    protected $_primary  = 'PerformanceDataXtraId';
    protected $_location;
    
    private function _init($id)
    {
        $locations = new Zend_Db_Table(array(
            'name' => 'view_locations', 
            'primary' => 'LocationId'));
        $this->_location = $locations->find((int) $id)->current();
        if(!$this->_location) throw new Exception($this->translate("Invalid location"));
    }
    
    public function __construct($id) 
    {
        parent::__construct();
        $this->_init($id);
        
        $this->setName('PerformanceDataXtra');
        
        $this->setAttrib('title', $this->translate("Performance data"));
        $this->setDescription($this->translate("Add performance data"));
        
        $pdxId = new Zend_Form_Element_Hidden('PerformanceDataXtraId');
        
        $locationId = new Zend_Form_Element_Hidden('LocationId');
        $locationId->setValue($this->_location->LocationId);
        
        $location = new Zend_Form_Element_Text('Location');
        $location->setLabel("Branch")
            ->setRequired(true)
            ->setAttrib('readonly', 'true')
            ->setValue($this->_location->Name);
        
        $sustainability = new Zend_Form_Element_Text('Sustainability');
        $sustainability->setLabel("Sustainability")
            ->setRequired(true)
            ->addValidator('NotEmpty', true, array('messages' => $this->translate("Required field, can't be empty")))
            ->addValidator('Regex', true, array(
                'pattern' => '(^\d+(\.\d{2})?$)',
                'messages' => $this->translate("Only numeric values allowed"))); 
        
        $submit = new Zend_Form_Element_Submit('Submit');
        $submit->setLabel("Submit")
            ->addDecorator(new HTech_Form_Decorator_Button(array('type'=>'submit')));
        
        $this->addElements(array($location,$sustainability,$submit,$locationId,$pdxId));        
    }
}